package com.sky.mapper;


import com.github.pagehelper.Page;
import com.sky.dto.GoodsSalesDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;


@Mapper
public interface OrderMapper {


    void insert(Orders orders);

    @Select("select * from orders where id = #{id}")
    Orders selectById(Long id);

    /**
     * 修改订单信息
     *
     * @param orders
     */
    void updateById(Orders orders);


    /**
     * 根据订单号查询订单
     *
     * @param orderNumber
     */
    @Select("select * from orders where number = #{orderNumber}")
    Orders getByNumber(String orderNumber);

    /**
     * 分页条件查询并按下单时间排序
     *
     * @param ordersPageQueryDTO
     */
    Page<Orders> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 根据订单状态和下单时间查询订单
     *
     * @param status
     * @param orderTime
     * @return
     */
    @Select("select * from orders where status = #{status} and order_time < #{orderTime}")
    List<Orders> getByStatusAndOrderTimeLT(Integer status, LocalDateTime orderTime);

    @Select("select * from orders where number = #{orderNumber} and user_id = #{loginIdAsLong}")
    Orders getByNumberAndUserId(String outTradeNo, long loginIdAsLong);

    /**
     * 根据动态条件统计营业额数据
     *
     * @param map
     * @return
     */
    Double sumByMap(Map map);

    /**
     * 根据id查询订单
     *
     * @param id
     */
    @Select("select * from orders where id=#{id}")
    Orders getById(Long id);

    /**
     * 根据状态统计订单数量
     *
     * @param status
     */

    @Select("select * from orders where number = #{orderNumber} and user_id = #{userId}")
    Orders getOrdersByNumberAndUserId(String orderNumber, long userId);

    @Select("select count(id) from orders")
    Integer countTotalOrders();

    @Select("select count(id) from orders where status = #{status}")
    Integer countByStatus(Integer completed);

    @Select("select SUM(amount) from orders where status = #{status}")
    Double sumByStatus(Integer completed);

    @Select("select count(id) from orders where status = #{status}")
    Integer countStatus(Integer status);

    /**
     * 修改订单信息
     *
     * @param orders
     */
    void update(Orders orders);

    @Select("select count(id) from orders")
    Integer getAllOrders();


    /**
     * 动态查询订单数
     *
     * @param map
     * @return
     */
    Integer selectOrderSum(Map map);

    @Select("select * from orders where status = 5 and order_time between #{begin} and #{end} ")
    List<Orders> getSalesTop10Orders(LocalDate begin, LocalDate end);

    @Select("select * from order_detail where order_id = #{id}")
    List<OrderDetail> getByOrdersId(Long id);


    @Select("SELECT name, SUM(number) as number FROM order_detail group by name order by number DESC LIMIT 10")
    List<GoodsSalesDTO> getTop10(List<OrderDetail> orderDetaillist);

    @Update("update orders set status = 2 where number = #{orderNumber}")
    void updateOrders(OrdersPaymentDTO ordersPaymentDTO);

    /**
     * 根据动态条件统计订单数量
     * @param map
     * @return
     */
    Integer countByMap(Map map);

}
